package Shopping;

import java.util.ArrayList;

public class ShoppingCart {
    ArrayList<Product> cart = new ArrayList<>();
    int maxCapacity = 5;
    public void addProduct(Product product) {
        if (cart.size() < maxCapacity) {
            cart.add(product);
            System.out.println(product.name + " 已添加到购物车！");
        } else {
            System.out.println("购物车已满，无法添加商品！");
        }
    }

    public void removeProduct(String productName) {
        for (int i = 0; i < cart.size(); i++) {
            if (cart.get(i).name.equals(productName)) {
                cart.remove(i);
                System.out.println(productName + " 已从购物车中移除！");
                return;
            }
        }
        System.out.println("购物车中没有找到 " + productName + "！");
    }

    public void viewCart() {
        if (cart.isEmpty()) {
            System.out.println("购物车为空！");
        } else {
            System.out.println("购物车中的商品：");
            for (Product p : cart) {
                p.display();
            }
        }
    }

    public void checkout() {
        if (cart.isEmpty()) {
            System.out.println("购物车为空，无法结算！");
        } else {
            double total = 0;
            for (Product p : cart) {
                total += p.price;
            }
            System.out.println("结算金额：¥" + total);
            cart.clear();
            System.out.println("购物车已清空，感谢购物！");
        }
    }
}
